package org.yellow.backend.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.yellow.backend.mapper.queueMapper;
import org.yellow.backend.pojo.Appointment;
import org.yellow.backend.pojo.patient;
import org.yellow.backend.pojo.queue;
import org.yellow.backend.utility.QueueStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: hy
 * @create: 2024-01-01
 */
@Service
public class queueService {
    @Autowired
    queueMapper mapper;

    @Autowired
    patientService patientService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 根据患者id查询排队信息
     * @param id
     * @return
     */
    public queue selectQueueById(int id){
        return mapper.selectById(id);
    }

    /**
     * 插入一个新患者到排队队列
     * @param patient
     * @return
     */
    public boolean insertNew(patient patient,int urgency){

        String url = "http://localhost:8888/appointment/get" +
                "DocId/"+patient.getPatientId();
        Long docId = restTemplate.getForObject(url, Long.class);
        queue Myqueue=null;
        if(urgency==1){
            Myqueue = new queue(null, patient.getPatientId(), docId, 10L, QueueStatus.Urgency);
        }
        else{
            Myqueue = new queue(null, patient.getPatientId(), docId,11L , QueueStatus.Active);
        }

        mapper.insert(Myqueue);

        return true;
    }

    /**
     * 挂起迟到未应号的患者
     * @param queueId
     * @return
     */
    public boolean updateStatus(int queueId){
        LambdaUpdateWrapper<queue> w = new LambdaUpdateWrapper<>();
        w.eq(queue::getQueueId,queueId).set(queue::getStatus,QueueStatus.Suspended);
        mapper.update(null,w);
        return true;
    }

    /**
     * 患者取消预约后移出相应队列
     * @param id
     * @return
     */
    public boolean deleteByPatientId(int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("patient_id",id);
        mapper.deleteByMap(map);
        return true;
    }


    /**
     * 返回排队信息
     * @return
     */
    public List<patient> showQueue(){
        return mapper.showQueueInter();
    }

    public List<Map<String,String>> showActiveQueue(int doctorId) {
        List<Appointment> appointments = mapper.showActiveQueue(doctorId);//所有符合条件的预约
        ArrayList<Map<String, String>> maps = new ArrayList<>();


        for (Appointment appointment :appointments){
            Map<String, String> map = new HashMap<>();
            Long patientId = appointment.getPatientId();
            String name = patientService.selectPatientById(patientId).getName();
            map.put("patientId",String.valueOf(appointment.getPatientId()));
            map.put("appointmentTime",String.valueOf(appointment.getAppointmentTime()));

            map.put("patientName",name);
            maps.add(map);
        }
        return maps ;
    }
}
